﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AssetManagementSystem.Utils;
using AssetManagementSystem.Model.IDao;

namespace AssetManagementSystem.Model {
    class AssetManager {

        IAssetDAO dao;

        public AssetManager() {
            //get the asset dao
            dao = DAOManager.GetAssetDAO();
        }

        //====================================================================
        //  STANDARD CRUD METHODS
        //====================================================================

        public void CreateAsset(Asset newAsset) {
            dao.CreateAsset(newAsset);
        }

        public List<Asset> GetAllAssets() {
            return dao.GetAllAssets().ToList<Asset>();
        }

        public Asset GetAsset(string id) {
            return dao.GetAsset(id);
        }

        public void UpdateAsset(Asset existingAsset) {
            dao.UpdateAsset(existingAsset);
        }

        public void DeleteAsset(Asset asset) {
            dao.DeleteAsset(asset);
        }

        public void DeleteAssetProperties(string assetId, Dictionary<string, string> properties) {
            //delete every property found in the dictionary
            foreach (KeyValuePair<string, string> property in properties) {
                dao.DeleteAssetProperty(assetId, property.Key);
            }
        }

        //====================================================================
        //  END - STANDARD CRUD METHODS
        //====================================================================

        //====================================================================
        //  ENHANCED METHODS
        //====================================================================

        /// <summary>
        /// Attempts to find assets contain part, or all of the input text in their string representations (ToString()).
        /// The results are sorted by relevance.
        /// </summary>
        /// <param name="input">The text to search for</param>
        /// <returns>The list of sorted results</returns>
        public List<Asset> FindAssets(string input) {
            //Get all assets
            List<Asset> allAssets = GetAllAssets();
            //Create a list to store results
            List<SortObject> sortList = new List<SortObject>();
            //Get all assets with at least one tri-gram match to the input
            foreach (Asset asset in allAssets) {
                int ngramMatches = SearchUtil.NGramSearch(input, asset.ToString(), 3);
                if(ngramMatches > 0){
                    sortList.Add(new SortObject(ngramMatches, asset));
                }
            }
            //Sort the results by the number of matches they each had
            sortList.Sort();
            //Turn the results back into a list of assets
            List<Asset> matchedAssets = new List<Asset>();
            foreach (SortObject obj in sortList) {
                matchedAssets.Add((Asset)obj.TheObject);
            }
            //return the sorted results
            return matchedAssets;
        }

        //====================================================================
        //  END - ENHANCED METHODS
        //====================================================================

    }
}
